package com.hequ.merchant.banner.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.hequ.merchant.banner.ApplicationHelper;
import com.hequ.merchant.banner.R;
import com.hequ.merchant.banner.adapter.ModuleAdapter;
import com.hequ.merchant.banner.api.ModuleDataListLoader;
import com.hequ.merchant.banner.entity.Module;
import com.hequ.merchant.banner.util.AppConstant;

/**
 * Created by 柯尊诚 on 2015/10/23.
 * kzc
 */
public class ModuleListActivity extends Activity {

    private ListView modulesList;
    private ModuleAdapter adapter;

    //public ArrayList<Module> moduless;
    private static Boolean isModuleManager;

    public static Boolean getIsModuleManager() {
        return isModuleManager;
    }

    public static void setIsModuleManager(Boolean isModuleManagers) {
        isModuleManager = isModuleManagers;
    }

    private ModuleDataListLoader getModuleList;

    private ModuleDataListLoader.OnDataRefreshListener onDataRefreshListener = new ModuleDataListLoader.OnDataRefreshListener() {
        @Override
        public void onRefresh() {
            adapter.notifyDataSetChanged();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.banner_list);

        modulesList = (ListView) findViewById(R.id.list_banner);

        getModuleList = ModuleDataListLoader.getInstance(this);

        //moduless = new ArrayList<>();
        //adapter = new ModuleAdapter(ModuleListActivity.this, moduless);
        adapter = new ModuleAdapter(ModuleListActivity.this, ApplicationHelper.getModules());

        if (isModuleManager) {
            modulesList.setOnItemClickListener(onClickModulesManager);
        } else {
            modulesList.setOnItemClickListener(onClickModulesItemforBanner);
        }
        modulesList.setAdapter(adapter);
    }

    @Override
    protected void onStart() {
        super.onStart();
        getModuleList.registerRefreshListener(onDataRefreshListener);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getModuleList.getList();
    }

    @Override
    protected void onStop() {
        super.onStop();
        getModuleList.unregisterRefreshListener(onDataRefreshListener);
    }

    private AdapterView.OnItemClickListener onClickModulesItemforBanner = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Module module = ApplicationHelper.getModules().get(position);
            Intent intent = new Intent(ModuleListActivity.this, BannerListActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("module", module);
            intent.putExtras(bundle);
            intent.putExtra("code", AppConstant.MOBANNER);
            startActivity(intent);
            Log.d("Item: ", module.toString());
        }
    };

    private AdapterView.OnItemClickListener onClickModulesManager = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Module module = ApplicationHelper.getModules().get(position);
            Intent intent = new Intent(ModuleListActivity.this, AddModuleActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("module", module);
            intent.putExtras(bundle);
            startActivity(intent);
            AddModuleActivity.isChange = true;
            Log.d("Item: ", module.toString());
        }
    };


}
